<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>thinkphp入坑 | 月藤的博客</title>
    <meta name="generator" content="VuePress 1.8.0">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.7.1/katex.min.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/github-markdown-css/2.10.0/github-markdown.min.css">
    <meta name="description" content="简单的介绍">
    
    <link rel="preload" href="/assets/css/0.styles.a2e941e0.css" as="style"><link rel="preload" href="/assets/js/app.54ea76a2.js" as="script"><link rel="preload" href="/assets/js/2.c554ece5.js" as="script"><link rel="preload" href="/assets/js/25.211ca3bf.js" as="script"><link rel="prefetch" href="/assets/js/10.95d63e51.js"><link rel="prefetch" href="/assets/js/11.26e082be.js"><link rel="prefetch" href="/assets/js/12.1f62021c.js"><link rel="prefetch" href="/assets/js/13.9096527c.js"><link rel="prefetch" href="/assets/js/14.86142c70.js"><link rel="prefetch" href="/assets/js/15.1adeebc5.js"><link rel="prefetch" href="/assets/js/16.efcc79e4.js"><link rel="prefetch" href="/assets/js/17.74da5698.js"><link rel="prefetch" href="/assets/js/18.fd808b3a.js"><link rel="prefetch" href="/assets/js/19.8dafe26f.js"><link rel="prefetch" href="/assets/js/20.aed54ab4.js"><link rel="prefetch" href="/assets/js/21.341a5670.js"><link rel="prefetch" href="/assets/js/22.ab8b375c.js"><link rel="prefetch" href="/assets/js/23.28489470.js"><link rel="prefetch" href="/assets/js/24.87aae001.js"><link rel="prefetch" href="/assets/js/26.afa4c8f0.js"><link rel="prefetch" href="/assets/js/27.9b98f6f3.js"><link rel="prefetch" href="/assets/js/28.93298733.js"><link rel="prefetch" href="/assets/js/29.e8c038c4.js"><link rel="prefetch" href="/assets/js/3.9d562ae1.js"><link rel="prefetch" href="/assets/js/30.27939f01.js"><link rel="prefetch" href="/assets/js/31.6a9774e8.js"><link rel="prefetch" href="/assets/js/32.f698e967.js"><link rel="prefetch" href="/assets/js/33.5ba4bfb8.js"><link rel="prefetch" href="/assets/js/34.dca90861.js"><link rel="prefetch" href="/assets/js/35.768c1bb2.js"><link rel="prefetch" href="/assets/js/36.6b0a867f.js"><link rel="prefetch" href="/assets/js/37.61156e90.js"><link rel="prefetch" href="/assets/js/38.e390246c.js"><link rel="prefetch" href="/assets/js/39.0fbd0dd1.js"><link rel="prefetch" href="/assets/js/4.e18bc6a8.js"><link rel="prefetch" href="/assets/js/40.4252699c.js"><link rel="prefetch" href="/assets/js/41.2e5b5840.js"><link rel="prefetch" href="/assets/js/42.8fe886c7.js"><link rel="prefetch" href="/assets/js/43.7a4b0851.js"><link rel="prefetch" href="/assets/js/44.93c2313d.js"><link rel="prefetch" href="/assets/js/45.33dcea60.js"><link rel="prefetch" href="/assets/js/46.681fdf10.js"><link rel="prefetch" href="/assets/js/47.a842a141.js"><link rel="prefetch" href="/assets/js/48.9a03ba74.js"><link rel="prefetch" href="/assets/js/49.a50266b1.js"><link rel="prefetch" href="/assets/js/5.dc6a2d8c.js"><link rel="prefetch" href="/assets/js/50.f2a42406.js"><link rel="prefetch" href="/assets/js/51.444cc3d8.js"><link rel="prefetch" href="/assets/js/52.cf2befd8.js"><link rel="prefetch" href="/assets/js/53.fbe609f8.js"><link rel="prefetch" href="/assets/js/54.a43fb514.js"><link rel="prefetch" href="/assets/js/55.d0c11641.js"><link rel="prefetch" href="/assets/js/56.c6f114d9.js"><link rel="prefetch" href="/assets/js/57.cc386420.js"><link rel="prefetch" href="/assets/js/58.e747d0f6.js"><link rel="prefetch" href="/assets/js/6.c1fd48f0.js"><link rel="prefetch" href="/assets/js/7.32b39b92.js"><link rel="prefetch" href="/assets/js/8.e2284671.js"><link rel="prefetch" href="/assets/js/9.8def3992.js">
    <link rel="stylesheet" href="/assets/css/0.styles.a2e941e0.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><!----> <span class="site-name">月藤的博客</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="https://ziphold.gitee.io/blog/#/" target="_blank" rel="noopener noreferrer" class="nav-link external">
  旧博客（停止更新）
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div><div class="nav-item"><a href="https://rattonlzh.github.io/homepage/homepage.html" target="_blank" rel="noopener noreferrer" class="nav-link external">
  常用网址导航（停止更新）
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="Select language" class="dropdown-title"><span class="title">Languages</span> <span class="arrow down"></span></button> <button type="button" aria-label="Select language" class="mobile-dropdown-title"><span class="title">Languages</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/知识点总结/thinkphp总结.html" class="nav-link">
  zh-CN
</a></li><li class="dropdown-item"><!----> <a href="/en/" class="nav-link">
  en-US
</a></li></ul></div></div> <!----></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><div class="avatar"><img src="/assets/img/avatar.2b77755b.png" alt srcset></div> <div style="z-index: 999"><iframe frameborder="no" border="0" marginwidth="0" marginheight="0" width="298" height="52" src="//music.163.com/outchain/player?type=2&id=28283137&auto=1&height=32"></iframe></div> <nav class="nav-links"><div class="nav-item"><a href="https://ziphold.gitee.io/blog/#/" target="_blank" rel="noopener noreferrer" class="nav-link external">
  旧博客（停止更新）
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div><div class="nav-item"><a href="https://rattonlzh.github.io/homepage/homepage.html" target="_blank" rel="noopener noreferrer" class="nav-link external">
  常用网址导航（停止更新）
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="Select language" class="dropdown-title"><span class="title">Languages</span> <span class="arrow down"></span></button> <button type="button" aria-label="Select language" class="mobile-dropdown-title"><span class="title">Languages</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/知识点总结/thinkphp总结.html" class="nav-link">
  zh-CN
</a></li><li class="dropdown-item"><!----> <a href="/en/" class="nav-link">
  en-US
</a></li></ul></div></div> <!----></nav>  <ul class="sidebar-links"><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>随便说说</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading open"><span>知识点总结</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/知识点总结/git总结.html" class="sidebar-link">git总结</a></li><li><a href="/知识点总结/java单元测试总结.html" class="sidebar-link">java测试</a></li><li><a href="/知识点总结/jquery总结.html" class="sidebar-link">jquery初探</a></li><li><a href="/知识点总结/js总结.html" class="sidebar-link">js易错点</a></li><li><a href="/知识点总结/kotlin语法总结.html" class="sidebar-link">kotlin语法入门</a></li><li><a href="/知识点总结/latex总结.html" class="sidebar-link">latex常用代码</a></li><li><a href="/知识点总结/PS总结.html" class="sidebar-link">PS总结</a></li><li><a href="/知识点总结/springboot总结.html" class="sidebar-link">springboot进行web开发总结</a></li><li><a href="/知识点总结/thinkphp总结.html" class="active sidebar-link">thinkphp入坑</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/知识点总结/thinkphp总结.html#了解目录-创建模块和入口文件" class="sidebar-link">了解目录，创建模块和入口文件</a></li><li class="sidebar-sub-header"><a href="/知识点总结/thinkphp总结.html#url访问" class="sidebar-link">url访问</a></li><li class="sidebar-sub-header"><a href="/知识点总结/thinkphp总结.html#控制器操作返回内容" class="sidebar-link">控制器操作返回内容</a></li><li class="sidebar-sub-header"><a href="/知识点总结/thinkphp总结.html#助手函数" class="sidebar-link">助手函数</a></li><li class="sidebar-sub-header"><a href="/知识点总结/thinkphp总结.html#定位模板文件" class="sidebar-link">定位模板文件</a></li><li class="sidebar-sub-header"><a href="/知识点总结/thinkphp总结.html#模板语法" class="sidebar-link">模板语法</a></li><li class="sidebar-sub-header"><a href="/知识点总结/thinkphp总结.html#布局模板" class="sidebar-link">布局模板</a></li><li class="sidebar-sub-header"><a href="/知识点总结/thinkphp总结.html#模板继承" class="sidebar-link">模板继承</a></li><li class="sidebar-sub-header"><a href="/知识点总结/thinkphp总结.html#页面包含" class="sidebar-link">页面包含</a></li><li class="sidebar-sub-header"><a href="/知识点总结/thinkphp总结.html#内置标签库" class="sidebar-link">内置标签库</a></li><li class="sidebar-sub-header"><a href="/知识点总结/thinkphp总结.html#模型类" class="sidebar-link">模型类</a></li></ul></li><li><a href="/知识点总结/uml软件建模总结.html" class="sidebar-link">uml软件建模总结</a></li><li><a href="/知识点总结/人工智能总结.html" class="sidebar-link">人工智能总结</a></li><li><a href="/知识点总结/大数据软件工程总结.html" class="sidebar-link">大数据软件工程总结</a></li><li><a href="/知识点总结/常用快捷键总结.html" class="sidebar-link">第一篇博客</a></li><li><a href="/知识点总结/数字图像处理总结.html" class="sidebar-link">数字图像处理知识点</a></li><li><a href="/知识点总结/数据库应用理论总结.html" class="sidebar-link">数据库应用复习</a></li><li><a href="/知识点总结/数据库系统概念总结.html" class="sidebar-link">数据库系统概念总结</a></li><li><a href="/知识点总结/数据挖掘总结.html" class="sidebar-link">数据挖掘总结</a></li><li><a href="/知识点总结/设计模式总结.html" class="sidebar-link">设计模式</a></li></ul></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>考研专区</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>读书笔记</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>开发资料</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>配置记录</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>思维导图</span> <span class="arrow right"></span></p> <!----></section></li><li><a href="/friend_link.html" class="sidebar-link">友情链接</a></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><h1 id="thinkphp总结"><a href="#thinkphp总结" class="header-anchor">#</a> thinkphp总结</h1> <p>与java相比，php是弱语言，动态语言，代码相对简洁，不需要等待编译，资源占用较小，适合中小项目尤其是个人项目开发，对新手入门较为友好，但在处理高并发方面上无能为力。</p> <p>第一行代码可以写 <code>print_r(phpinfo())</code>，可以看到php，apache，系统环境变量，php变量等巨多的配置信息</p> <p>下载thinkphp5框架thinkphp_5.0.24_with_extend.zip，解压后文件目录如下</p> <p><img src="/assets/img/image-20200222121107424.19c639c8.png" alt="image-20200222121107424"></p> <h2 id="了解目录-创建模块和入口文件"><a href="#了解目录-创建模块和入口文件" class="header-anchor">#</a> 了解目录，创建模块和入口文件</h2> <p>thinkphp是框架文件，必须要存在，但开发者不需要修改此目录，application是默认的应用目录，开发者在该目录下编写业务逻辑代码，应用目录是在入口文件或think文件定义 <code>APP_PATH</code>确定的</p> <div class="language- extra-class"><pre class="language-text"><code>// 定义项目路径
define('APP_PATH', __DIR__ . '/application/');
</code></pre></div><p>runtime目录保存运行时产生的文件如文件日志等</p> <p>命令行运行build.php可以自动生成新模块，如新建forum模块，在项目根目录运行命令</p> <div class="language- extra-class"><pre class="language-text"><code>php think build --module=forum
</code></pre></div><p>运行结果在应用目录生成forum模块，包含controller，model和view三个文件夹，在controller文件夹默认生成Index控制器的类文件，view文件夹默认生成index.html</p> <p>默认thinkphp会从应用目录下查找build.php, 所以为了不报 <code>No Such File or Directory</code>的异常，还得把 <code>build.php</code>移到应用目录下</p> <p>但仍旧需要手动编写入口文件，入口文件参考public目录下的index.php, 为了尽可能保持url的简洁，最好将入口文件移到站点根目录,内容也要做相应更改</p> <div class="language- extra-class"><pre class="language-text"><code>// 定义应用目录
define('APP_PATH',  './app/');
// 加载框架引导文件
require './thinkphp/start.php';
</code></pre></div><p>可能你应用目录有多个模块，但是只想暴露其中几个模块给用户（其他模块可能是公共类库），那么你可以一个入口绑定一个模块，在入口文件添加一行代码</p> <div class="language- extra-class"><pre class="language-text"><code>define('BIND_MODULE','index');// index是你要绑定的模块
</code></pre></div><p>那么通过该入口文件，url只能解析index模块</p> <h2 id="url访问"><a href="#url访问" class="header-anchor">#</a> url访问</h2> <p>典型的访问规则</p> <h4 id="http-servername-index-php-或者其它应用入口文件-模块-控制器-操作-参数名-参数值"><a href="#http-servername-index-php-或者其它应用入口文件-模块-控制器-操作-参数名-参数值" class="header-anchor">#</a> <code>http://serverName/index.php（或者其它应用入口文件）/模块/控制器/操作/[参数名/参数值...]</code></h4> <p>如果index.php绑定了某模块，那么模块名可以省略</p> <p>默认url是不区分大小写的</p> <p>如果控制器类名采用驼峰命名法，则在url中控制器名的单词间用下划线连接，如 <code>HomeBase</code>类在URL中写成 <code>home_base</code></p> <h2 id="控制器操作返回内容"><a href="#控制器操作返回内容" class="header-anchor">#</a> 控制器操作返回内容</h2> <p><code>application/config.php</code>定义了控制器方法的默认返回类型</p> <div class="language- extra-class"><pre class="language-text"><code>'default_return_type'    =&gt; 'html',
</code></pre></div><p>也可以在控制其方法return的时候使用助手函数手动改变返回的内容类型</p> <p>json： <code>return json(['data'=&gt;$data,'code'=&gt;1,'message'=&gt;'操作完成']);</code></p> <p>xml： <code>return xml(['data'=&gt;$data,'code'=&gt;1,'message'=&gt;'操作完成']);</code></p> <p>jsonp: <code>return jsonp(['data'=&gt;$data,'code'=&gt;1’, ‘callback’=&gt;‘2979’]);</code></p> <p>html: <code>return view('&lt;h1&gt;wwww&lt;/h1&gt;');</code></p> <h2 id="助手函数"><a href="#助手函数" class="header-anchor">#</a> 助手函数</h2> <p>除了上面提到的几个，还有大量有用的助手函数</p> <table><thead><tr><th style="text-align:left;">助手函数</th> <th style="text-align:left;">描述</th></tr></thead> <tbody><tr><td style="text-align:left;">abort</td> <td style="text-align:left;">中断执行并发送HTTP状态码</td></tr> <tr><td style="text-align:left;">action</td> <td style="text-align:left;">调用控制器类的操作</td></tr> <tr><td style="text-align:left;">cache</td> <td style="text-align:left;">缓存管理</td></tr> <tr><td style="text-align:left;">config</td> <td style="text-align:left;">获取和设置配置参数</td></tr> <tr><td style="text-align:left;">controller</td> <td style="text-align:left;">实例化控制器</td></tr> <tr><td style="text-align:left;">cookie</td> <td style="text-align:left;">Cookie管理</td></tr> <tr><td style="text-align:left;">db</td> <td style="text-align:left;">实例化数据库类</td></tr> <tr><td style="text-align:left;">debug</td> <td style="text-align:left;">调试时间和内存占用</td></tr> <tr><td style="text-align:left;">dump</td> <td style="text-align:left;">浏览器友好的变量输出</td></tr> <tr><td style="text-align:left;">exception</td> <td style="text-align:left;">抛出异常处理</td></tr> <tr><td style="text-align:left;">halt</td> <td style="text-align:left;">变量调试输出并中断执行</td></tr> <tr><td style="text-align:left;">import</td> <td style="text-align:left;">导入所需的类库</td></tr> <tr><td style="text-align:left;">input</td> <td style="text-align:left;">获取输入数据 支持默认值和过滤</td></tr> <tr><td style="text-align:left;">json</td> <td style="text-align:left;">JSON数据输出</td></tr> <tr><td style="text-align:left;">jsonp</td> <td style="text-align:left;">JSONP数据输出</td></tr> <tr><td style="text-align:left;">lang</td> <td style="text-align:left;">获取语言变量值</td></tr> <tr><td style="text-align:left;">load_trait</td> <td style="text-align:left;">快速导入Traits <code>PHP5.5</code>以上无需调用</td></tr> <tr><td style="text-align:left;">model</td> <td style="text-align:left;">实例化Model</td></tr> <tr><td style="text-align:left;">redirect</td> <td style="text-align:left;">重定向输出</td></tr> <tr><td style="text-align:left;">request</td> <td style="text-align:left;">实例化Request对象</td></tr> <tr><td style="text-align:left;">response</td> <td style="text-align:left;">实例化Response对象</td></tr> <tr><td style="text-align:left;">session</td> <td style="text-align:left;">Session管理</td></tr> <tr><td style="text-align:left;">trace</td> <td style="text-align:left;">记录日志信息</td></tr> <tr><td style="text-align:left;">token</td> <td style="text-align:left;">生成表单令牌输出</td></tr> <tr><td style="text-align:left;">url</td> <td style="text-align:left;">Url生成</td></tr> <tr><td style="text-align:left;">validate</td> <td style="text-align:left;">实例化验证器</td></tr> <tr><td style="text-align:left;">vendor</td> <td style="text-align:left;">快速导入第三方框架类库</td></tr> <tr><td style="text-align:left;">view</td> <td style="text-align:left;">渲染模板输出</td></tr> <tr><td style="text-align:left;">widget</td> <td style="text-align:left;">渲染输出Widget</td></tr> <tr><td style="text-align:left;">xml</td> <td style="text-align:left;">XML数据输出</td></tr></tbody></table> <ul><li>抛出异常</li></ul> <div class="language- extra-class"><pre class="language-text"><code>abort(404,'页面不存在');
</code></pre></div><h2 id="定位模板文件"><a href="#定位模板文件" class="header-anchor">#</a> 定位模板文件</h2> <div class="language-php extra-class"><pre class="language-php"><code><span class="token keyword">return</span> <span class="token variable">$view</span><span class="token operator">-&gt;</span><span class="token function">fetch</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//[模板文件目录]/当前控制器名（小写+下划线）/当前操作名（小写）.html</span>
<span class="token keyword">return</span> <span class="token variable">$view</span><span class="token operator">-&gt;</span><span class="token function">fetch</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'add'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">return</span> <span class="token variable">$view</span><span class="token operator">-&gt;</span><span class="token function">fetch</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'user/add'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">return</span> <span class="token variable">$view</span><span class="token operator">-&gt;</span><span class="token function">fetch</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'admin@user/add'</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//跨模块调用视图</span>
</code></pre></div><h2 id="模板语法"><a href="#模板语法" class="header-anchor">#</a> 模板语法</h2> <p>打印变量 <code>{$ 变量名}</code></p> <div class="language- extra-class"><pre class="language-text"><code>Hello,{$name}！
</code></pre></div><p>等同于</p> <div class="language- extra-class"><pre class="language-text"><code>Hello,&lt;?php echo($name);?&gt;！
</code></pre></div><p>输出系统变量</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token punctuation">{</span>$Think<span class="token punctuation">.</span>server<span class="token punctuation">.</span>script_name<span class="token punctuation">}</span> <span class="token comment">// 输出$_SERVER['SCRIPT_NAME']变量</span>
<span class="token punctuation">{</span>$Think<span class="token punctuation">.</span>session<span class="token punctuation">.</span>user_id<span class="token punctuation">}</span> <span class="token comment">// 输出$_SESSION['user_id']变量</span>
<span class="token punctuation">{</span>$Think<span class="token punctuation">.</span>get<span class="token punctuation">.</span>pageNumber<span class="token punctuation">}</span> <span class="token comment">// 输出$_GET['pageNumber']变量</span>
<span class="token punctuation">{</span>$Think<span class="token punctuation">.</span>cookie<span class="token punctuation">.</span>name<span class="token punctuation">}</span>  <span class="token comment">// 输出$_COOKIE['name']变量</span>
</code></pre></div><p>输出常量</p> <div class="language- extra-class"><pre class="language-text"><code>{$Think.const.APP_PATH}或{$Think.APP_PATH}
</code></pre></div><p>调用请求方法</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// 调用Request对象的get方法 传入参数为id</span>
<span class="token punctuation">{</span>$Request<span class="token punctuation">.</span>get<span class="token punctuation">.</span>id<span class="token punctuation">}</span>
<span class="token comment">// 调用Request对象的param方法 传入参数为name</span>
<span class="token punctuation">{</span>$Request<span class="token punctuation">.</span>param<span class="token punctuation">.</span>name<span class="token punctuation">}</span>
<span class="token comment">// 调用Request对象的param方法 传入参数为user.nickname</span>
<span class="token punctuation">{</span>$Request<span class="token punctuation">.</span>param<span class="token punctuation">.</span>user<span class="token punctuation">.</span>nickname<span class="token punctuation">}</span>
<span class="token comment">// 调用Request对象的root方法</span>
<span class="token punctuation">{</span>$Request<span class="token punctuation">.</span>root<span class="token punctuation">}</span>
<span class="token comment">// 调用Request对象的root方法，并且传入参数true</span>
<span class="token punctuation">{</span>$Request<span class="token punctuation">.</span>root<span class="token punctuation">.</span><span class="token boolean">true</span><span class="token punctuation">}</span>
<span class="token comment">// 调用Request对象的path方法</span>
<span class="token punctuation">{</span>$Request<span class="token punctuation">.</span>path<span class="token punctuation">}</span>
<span class="token comment">// 调用Request对象的module方法</span>
<span class="token punctuation">{</span>$Request<span class="token punctuation">.</span>module<span class="token punctuation">}</span>
<span class="token comment">// 调用Request对象的controller方法</span>
<span class="token punctuation">{</span>$Request<span class="token punctuation">.</span>controller<span class="token punctuation">}</span>
<span class="token comment">// 调用Request对象的action方法</span>
<span class="token punctuation">{</span>$Request<span class="token punctuation">.</span>action<span class="token punctuation">}</span>
<span class="token comment">// 调用Request对象的ext方法</span>
<span class="token punctuation">{</span>$Request<span class="token punctuation">.</span>ext<span class="token punctuation">}</span>
<span class="token comment">// 调用Request对象的host方法</span>
<span class="token punctuation">{</span>$Request<span class="token punctuation">.</span>host<span class="token punctuation">}</span>
<span class="token comment">// 调用Request对象的ip方法</span>
<span class="token punctuation">{</span>$Request<span class="token punctuation">.</span>ip<span class="token punctuation">}</span>
<span class="token comment">// 调用Request对象的header方法</span>
<span class="token punctuation">{</span>$Request<span class="token punctuation">.</span>header<span class="token punctuation">.</span>accept<span class="token operator">-</span>encoding<span class="token punctuation">}</span>
</code></pre></div><p>输出函数执行的结果</p> <div class="language- extra-class"><pre class="language-text"><code>{:substr(strtoupper(md5($name)),0,3)}
</code></pre></div><p><img src="/assets/img/image-20200222210914555.f3d7c779.png" alt="image-20200222210914555"></p> <p>三元运算符</p> <div class="language- extra-class"><pre class="language-text"><code>{$status? '正常' : '错误'}
</code></pre></div><div class="language-js extra-class"><pre class="language-js"><code><span class="token punctuation">{</span>$varname<span class="token punctuation">.</span>aa <span class="token operator">??</span> <span class="token string">'xxx'</span><span class="token punctuation">}</span><span class="token comment">// $varname.aa有设置则输出$varname.aa，否则输出xxx</span>
</code></pre></div><div class="language-js extra-class"><pre class="language-js"><code><span class="token punctuation">{</span>$varname<span class="token operator">?</span><span class="token operator">=</span><span class="token string">'xxx'</span><span class="token punctuation">}</span> <span class="token comment">// 如果$varname为真，则输出xxx</span>
</code></pre></div><div class="language-js extra-class"><pre class="language-js"><code><span class="token punctuation">{</span>$varname <span class="token operator">?</span><span class="token operator">:</span> <span class="token string">'no'</span><span class="token punctuation">}</span><span class="token comment">// 如果$varname为真，则输出$varname,否则输出no</span>
</code></pre></div><div class="language- extra-class"><pre class="language-text"><code>{literal}
    Hello,{$name}！
{/literal}
</code></pre></div><p>literal标签之间的内容不会被解析，而是原样输出</p> <h2 id="布局模板"><a href="#布局模板" class="header-anchor">#</a> 布局模板</h2> <h3 id="配置文件启用全局模板"><a href="#配置文件启用全局模板" class="header-anchor">#</a> 配置文件启用全局模板</h3> <div class="language-php extra-class"><pre class="language-php"><code><span class="token string single-quoted-string">'template'</span>  <span class="token operator">=&gt;</span>  <span class="token punctuation">[</span>
    <span class="token string single-quoted-string">'layout_on'</span>     <span class="token operator">=&gt;</span>  <span class="token constant boolean">true</span><span class="token punctuation">,</span><span class="token comment">// 默认为false</span>
    <span class="token string single-quoted-string">'layout_name'</span>   <span class="token operator">=&gt;</span>  <span class="token string single-quoted-string">'layout'</span><span class="token punctuation">,</span><span class="token comment">//默认</span>
    <span class="token string single-quoted-string">'layout_item'</span>   <span class="token operator">=&gt;</span>  <span class="token string single-quoted-string">'{CONTENT}'</span><span class="token comment">//默认</span>
<span class="token punctuation">]</span>
</code></pre></div><h3 id="取消布局"><a href="#取消布局" class="header-anchor">#</a> 取消布局</h3> <p>在模板文件开头加上 <code>{__NOLAYOUT__}</code>，则不会进行布局模板解析</p> <h3 id="手动指定模板以及替换字符串-可选"><a href="#手动指定模板以及替换字符串-可选" class="header-anchor">#</a> 手动指定模板以及替换字符串（可选）</h3> <div class="language- extra-class"><pre class="language-text"><code>{layout name=&quot;Layout/newlayout&quot; replace=&quot;[__REPLACE__]&quot; /}
</code></pre></div><p>如果没有关闭全局模板可能会出现闭循环</p> <h3 id="控制器方法种指定布局"><a href="#控制器方法种指定布局" class="header-anchor">#</a> 控制器方法种指定布局</h3> <div class="language-php extra-class"><pre class="language-php"><code><span class="token keyword">namespace</span> <span class="token package">app<span class="token punctuation">\</span>index<span class="token punctuation">\</span>controller</span><span class="token punctuation">;</span>

<span class="token keyword">use</span> <span class="token package">think<span class="token punctuation">\</span>Controller</span><span class="token punctuation">;</span>

<span class="token keyword">class</span> <span class="token class-name">User</span> <span class="token keyword">extends</span> <span class="token class-name">Controller</span>
<span class="token punctuation">{</span>
     <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">add</span><span class="token punctuation">(</span><span class="token punctuation">)</span> 
     <span class="token punctuation">{</span>
         <span class="token variable">$this</span><span class="token operator">-&gt;</span><span class="token property">view</span><span class="token operator">-&gt;</span><span class="token property">engine</span><span class="token operator">-&gt;</span><span class="token function">layout</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Layout/newlayout'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
         <span class="token keyword">return</span> <span class="token variable">$this</span><span class="token operator">-&gt;</span><span class="token function">display</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'add'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
     <span class="token punctuation">}</span>
    
     <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">add2</span><span class="token punctuation">(</span><span class="token punctuation">)</span> 
     <span class="token punctuation">{</span>
        <span class="token comment">// 临时关闭当前模板的布局功能</span>
         <span class="token variable">$this</span><span class="token operator">-&gt;</span><span class="token property">view</span><span class="token operator">-&gt;</span><span class="token property">engine</span><span class="token operator">-&gt;</span><span class="token function">layout</span><span class="token punctuation">(</span><span class="token constant boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">;</span> 
         <span class="token keyword">return</span> <span class="token variable">$this</span><span class="token operator">-&gt;</span><span class="token function">display</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'add2'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
     <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><h2 id="模板继承"><a href="#模板继承" class="header-anchor">#</a> 模板继承</h2> <div class="language-html extra-class"><pre class="language-html"><code><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>html</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>head</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>meta</span> <span class="token attr-name">http-equiv</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>Content-Type<span class="token punctuation">&quot;</span></span> <span class="token attr-name">content</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>text/html; charset=utf-8<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>title</span><span class="token punctuation">&gt;</span></span>{block name=&quot;title&quot;}标题{/block}<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>title</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>head</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>body</span><span class="token punctuation">&gt;</span></span>
{block name=&quot;menu&quot;}菜单{/block}
{block name=&quot;left&quot;}左边分栏{/block}
{block name=&quot;main&quot;}主内容{/block}
{block name=&quot;right&quot;}右边分栏{/block}
{block name=&quot;footer&quot;}底部{/block}
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>body</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>html</span><span class="token punctuation">&gt;</span></span>
</code></pre></div><div class="language-html extra-class"><pre class="language-html"><code>{extend name=&quot;base&quot; /}
{block name=&quot;title&quot;}{$title}{/block}
{block name=&quot;menu&quot;}
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>a</span> <span class="token attr-name">href</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>/<span class="token punctuation">&quot;</span></span> <span class="token punctuation">&gt;</span></span>首页<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>a</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>a</span> <span class="token attr-name">href</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>/info/<span class="token punctuation">&quot;</span></span> <span class="token punctuation">&gt;</span></span>资讯<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>a</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>a</span> <span class="token attr-name">href</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>/bbs/<span class="token punctuation">&quot;</span></span> <span class="token punctuation">&gt;</span></span>论坛<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>a</span><span class="token punctuation">&gt;</span></span>
{/block}
{block name=&quot;left&quot;}{/block}
{block name=&quot;main&quot;}
{volist name=&quot;list&quot; id=&quot;vo&quot;}
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>a</span> <span class="token attr-name">href</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>/new/{$vo.id}<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>{$vo.title}<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>a</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>br</span><span class="token punctuation">/&gt;</span></span>
 {$vo.content}
{/volist}
{/block}
{block name=&quot;right&quot;}
 最新资讯：
{volist name=&quot;news&quot; id=&quot;new&quot;}
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>a</span> <span class="token attr-name">href</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>/new/{$new.id}<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>{$new.title}<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>a</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>br</span><span class="token punctuation">/&gt;</span></span>
{/volist}
{/block}
{block name=&quot;footer&quot;}
{__block__}
 @ThinkPHP 版权所有
{/block}
</code></pre></div><h3 id="合并父子模板的block内容"><a href="#合并父子模板的block内容" class="header-anchor">#</a> 合并父子模板的block内容</h3> <div class="language- extra-class"><pre class="language-text"><code>{block name=&quot;footer&quot;}
{__block__}@ThinkPHP 版权所有
{/block}
</code></pre></div><p>{<strong>block</strong>}被替换为父模板相应block的内容</p> <p>子模板的页面元素必须放在block中，否则会被忽略</p> <h2 id="页面包含"><a href="#页面包含" class="header-anchor">#</a> 页面包含</h2> <div class="language- extra-class"><pre class="language-text"><code>{include file=&quot;public/header,public/menu&quot; /}
</code></pre></div><p>被包含文件不能使用页面布局或模板继承</p> <div class="language- extra-class"><pre class="language-text"><code>{include file=&quot;Public/header&quot; title=&quot;$title&quot; keywords=&quot;开源WEB开发框架&quot; /}
</code></pre></div><p>包含时可以传入参数</p> <h2 id="内置标签库"><a href="#内置标签库" class="header-anchor">#</a> 内置标签库</h2> <div class="language- extra-class"><pre class="language-text"><code>{eq name=&quot;status&quot; value=&quot;1&quot; }
正常
{/eq}
</code></pre></div><table><thead><tr><th style="text-align:left;">标签名</th> <th style="text-align:left;">作用</th> <th style="text-align:left;">包含属性</th></tr></thead> <tbody><tr><td style="text-align:left;">include</td> <td style="text-align:left;">包含外部模板文件（闭合）</td> <td style="text-align:left;">file</td></tr> <tr><td style="text-align:left;">load</td> <td style="text-align:left;">导入资源文件（闭合 包括js css import别名）</td> <td style="text-align:left;">file,href,type,value,basepath</td></tr> <tr><td style="text-align:left;">volist</td> <td style="text-align:left;">循环数组数据输出</td> <td style="text-align:left;">name,id,offset,length,key,mod</td></tr> <tr><td style="text-align:left;">foreach</td> <td style="text-align:left;">数组或对象遍历输出</td> <td style="text-align:left;">name,item,key</td></tr> <tr><td style="text-align:left;">for</td> <td style="text-align:left;">For循环数据输出</td> <td style="text-align:left;">name,from,to,before,step</td></tr> <tr><td style="text-align:left;">switch</td> <td style="text-align:left;">分支判断输出</td> <td style="text-align:left;">name</td></tr> <tr><td style="text-align:left;">case</td> <td style="text-align:left;">分支判断输出（必须和switch配套使用）</td> <td style="text-align:left;">value,break</td></tr> <tr><td style="text-align:left;">default</td> <td style="text-align:left;">默认情况输出（闭合 必须和switch配套使用）</td> <td style="text-align:left;">无</td></tr> <tr><td style="text-align:left;">compare</td> <td style="text-align:left;">比较输出（包括eq neq lt gt egt elt heq nheq等别名）</td> <td style="text-align:left;">name,value,type</td></tr> <tr><td style="text-align:left;">range</td> <td style="text-align:left;">范围判断输出（包括in notin between notbetween别名）</td> <td style="text-align:left;">name,value,type</td></tr> <tr><td style="text-align:left;">present</td> <td style="text-align:left;">判断是否赋值</td> <td style="text-align:left;">name</td></tr> <tr><td style="text-align:left;">notpresent</td> <td style="text-align:left;">判断是否尚未赋值</td> <td style="text-align:left;">name</td></tr> <tr><td style="text-align:left;">empty</td> <td style="text-align:left;">判断数据是否为空</td> <td style="text-align:left;">name</td></tr> <tr><td style="text-align:left;">notempty</td> <td style="text-align:left;">判断数据是否不为空</td> <td style="text-align:left;">name</td></tr> <tr><td style="text-align:left;">defined</td> <td style="text-align:left;">判断常量是否定义</td> <td style="text-align:left;">name</td></tr> <tr><td style="text-align:left;">notdefined</td> <td style="text-align:left;">判断常量是否未定义</td> <td style="text-align:left;">name</td></tr> <tr><td style="text-align:left;">define</td> <td style="text-align:left;">常量定义（闭合）</td> <td style="text-align:left;">name,value</td></tr> <tr><td style="text-align:left;">assign</td> <td style="text-align:left;">变量赋值（闭合）</td> <td style="text-align:left;">name,value</td></tr> <tr><td style="text-align:left;">if</td> <td style="text-align:left;">条件判断输出</td> <td style="text-align:left;">condition</td></tr> <tr><td style="text-align:left;">elseif</td> <td style="text-align:left;">条件判断输出（闭合 必须和if标签配套使用）</td> <td style="text-align:left;">condition</td></tr> <tr><td style="text-align:left;">else</td> <td style="text-align:left;">条件不成立输出（闭合 可用于其他标签）</td> <td style="text-align:left;">无</td></tr> <tr><td style="text-align:left;">php</td> <td style="text-align:left;">使用php代码</td> <td style="text-align:left;">无</td></tr></tbody></table> <h3 id="循环输出"><a href="#循环输出" class="header-anchor">#</a> 循环输出</h3> <div class="language- extra-class"><pre class="language-text"><code>{volist name=&quot;list&quot; id=&quot;vo&quot;}
{$vo.id}:{$vo.name}&lt;br/&gt;
{/volist}
</code></pre></div><h3 id="比较"><a href="#比较" class="header-anchor">#</a> 比较</h3> <table><thead><tr><th style="text-align:left;">标签</th> <th style="text-align:left;">含义</th></tr></thead> <tbody><tr><td style="text-align:left;">eq或者 equal</td> <td style="text-align:left;">等于</td></tr> <tr><td style="text-align:left;">neq 或者notequal</td> <td style="text-align:left;">不等于</td></tr> <tr><td style="text-align:left;">gt</td> <td style="text-align:left;">大于</td></tr> <tr><td style="text-align:left;">egt</td> <td style="text-align:left;">大于等于</td></tr> <tr><td style="text-align:left;">lt</td> <td style="text-align:left;">小于</td></tr> <tr><td style="text-align:left;">elt</td> <td style="text-align:left;">小于等于</td></tr> <tr><td style="text-align:left;">heq</td> <td style="text-align:left;">恒等于</td></tr> <tr><td style="text-align:left;">nheq</td> <td style="text-align:left;">不恒等于</td></tr></tbody></table> <div class="language- extra-class"><pre class="language-text"><code>{比较标签 name=&quot;变量&quot; value=&quot;值&quot;}
内容
{/比较标签}
</code></pre></div><p>变量与值比较，如果比较结果为真，则输出内容</p> <div class="language- extra-class"><pre class="language-text"><code>{eq name=&quot;name&quot; value=&quot;value&quot;}
相等
{else/}
不相等
{/eq}
</code></pre></div><h3 id="条件"><a href="#条件" class="header-anchor">#</a> 条件</h3> <div class="language- extra-class"><pre class="language-text"><code>{switch name=&quot;变量&quot; }
    {case value=&quot;值1&quot; break=&quot;0或1&quot;}输出内容1{/case}
    {case value=&quot;值2&quot;}输出内容2{/case}
    {default /}默认情况
{/switch}
</code></pre></div><div class="language- extra-class"><pre class="language-text"><code>{if condition=&quot;($name == 1) OR ($name &gt; 100) &quot;} value1
{elseif condition=&quot;$name eq 2&quot;/}value2
{else /} value3
{/if}
</code></pre></div><h3 id="范围"><a href="#范围" class="header-anchor">#</a> 范围</h3> <div class="language- extra-class"><pre class="language-text"><code>{in name=&quot;id&quot; value=&quot;1,2,3&quot;}
id在范围内
{/in}
</code></pre></div><p>可以使用between标签来判断变量是否在某个区间范围内，可以使用：</p> <div class="language- extra-class"><pre class="language-text"><code>{between name=&quot;id&quot; value=&quot;1,10&quot;}
输出内容1
{/between}
</code></pre></div><p>同样，也可以使用notbetween标签来判断变量不在某个范围内：</p> <div class="language- extra-class"><pre class="language-text"><code>{notbetween name=&quot;id&quot; value=&quot;1,10&quot;}
输出内容2
{/notbetween}
</code></pre></div><p>空值与未定义</p> <div class="language- extra-class"><pre class="language-text"><code>{empty name=&quot;name&quot;}
name为空值
{/empty}
</code></pre></div><div class="language- extra-class"><pre class="language-text"><code>{notempty name=&quot;name&quot;}
name不为空
{/notempty}
</code></pre></div><div class="language- extra-class"><pre class="language-text"><code>{defined name=&quot;NAME&quot;}
NAME常量已经定义
{/defined}
</code></pre></div><div class="language- extra-class"><pre class="language-text"><code>{notdefined name=&quot;NAME&quot;}
NAME常量未定义
{/notdefined}
</code></pre></div><p>加载js与css</p> <div class="language- extra-class"><pre class="language-text"><code>{load href=&quot;/static/js/common.js&quot; /}
{load href=&quot;/static/css/style.css&quot; /}
</code></pre></div><p>原生php</p> <div class="language- extra-class"><pre class="language-text"><code>{php}echo 'Hello,world!';{/php}
</code></pre></div><p>数据库增删查改</p> <p>注意：使用db助手函数默认每次都会重新连接数据库，而使用<code>Db::name</code>或者<code>Db::table</code>方法的话都是单例的。</p> <div class="language- extra-class"><pre class="language-text"><code>db('user')-&gt;where('id',1)-&gt;find();
db('user')-&gt;where('status',1)-&gt;select();
</code></pre></div><div class="language- extra-class"><pre class="language-text"><code>// 添加单条数据
db('user')-&gt;insert($data);

// 添加多条数据
db('user')-&gt;insertAll($list);
</code></pre></div><div class="language- extra-class"><pre class="language-text"><code>// 更新数据表中的数据
db('user')-&gt;where('id',1)-&gt;update(['name' =&gt; 'thinkphp']);
// 更新某个字段的值
db('user')-&gt;where('id',1)-&gt;setField('name','thinkphp');
// 自增 score 字段
db('user')-&gt;where('id', 1)-&gt;setInc('score');
// 自减 score 字段
db('user')-&gt;where('id', 1)-&gt;setDec('score');
</code></pre></div><div class="language- extra-class"><pre class="language-text"><code>// 根据主键删除
db('user')-&gt;delete(1);
// 条件删除    
db('user')-&gt;where('id',1)-&gt;delete();
</code></pre></div><p>视图查询（不依赖数据库视图的多表查询）</p> <div class="language- extra-class"><pre class="language-text"><code>Db::view('User','id,name')
    -&gt;view('Profile','truename,phone,email','Profile.user_id=User.id')
    -&gt;view('Score','score','Score.user_id=Profile.id')
    -&gt;where('score','&gt;',80)
    -&gt;select();
</code></pre></div><h2 id="模型类"><a href="#模型类" class="header-anchor">#</a> 模型类</h2> <div class="language- extra-class"><pre class="language-text"><code>namespace app\index\model;

use think\Model;

class User extends Model
{
}
</code></pre></div><p>默认主键为自动识别，如果需要指定，可以设置属性：</p> <div class="language- extra-class"><pre class="language-text"><code>namespace app\index\model;

use think\Model;

class User extends Model
{
    protected $pk = 'uid';
}
</code></pre></div><div class="language- extra-class"><pre class="language-text"><code>// 静态调用
$user = User::get(1);
$user-&gt;name = 'thinkphp';
$user-&gt;save();

// 实例化模型
$user = new User;
$user-&gt;name= 'thinkphp';
$user-&gt;save();

// 使用 Loader 类实例化（单例）
$user = Loader::model('User');

// 或者使用助手函数`model`
$user = model('User');
$user-&gt;name= 'thinkphp';
$user-&gt;save();
</code></pre></div><p>模型 新增数据</p> <div class="language- extra-class"><pre class="language-text"><code>$user = new User;
$user-&gt;data([
    'name'  =&gt;  'thinkphp',
    'email' =&gt;  'thinkphp@qq.com'
]);
$user-&gt;save();
</code></pre></div></div> <footer class="page-edit"><!----> <!----> <a rel="license" href="https://creativecommons.org/licenses/by-sa/4.0/deed.zh"><img alt="知识共享许可协议" src="" style="border-width:0"></a><br>本作品采用<a rel="license" href="http://creativecommons.org/licenses/by/4.0/">知识共享署名 4.0 国际许可协议</a>进行许可。

   
</footer> <div class="page-nav"><p class="inner"><span class="prev">
      ←
      <a href="/知识点总结/springboot总结.html" class="prev">
        springboot进行web开发总结
      </a></span> <span class="next"><a href="/知识点总结/uml软件建模总结.html">
        uml软件建模总结
      </a>
      →
    </span></p></div>  <footer style="text-align:center;"><a href="https://beian.miit.gov.cn">
    粤ICP备2021020303号</a></footer></main></div><div class="global-ui"><!----></div></div>
    <script src="/assets/js/app.54ea76a2.js" defer></script><script src="/assets/js/2.c554ece5.js" defer></script><script src="/assets/js/25.211ca3bf.js" defer></script>
  </body>
</html>
